package com.csti.delio.main.client.login;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.event.dom.client.KeyCodes;

public class UsuarioForm extends Dialog{

    TextField<String> us;
    TextField<String> ps;
    Button ac,cn;
    FormPanel f;
    private Status status;
    
    public UsuarioForm(){
    	
    	GXT.hideLoadingPanel("loading");
       	addForm();
        setBodyBorder(false);  
	    setHeading("Login");
	    setIconStyle("images/icons/user.png");
	    setWidth(280);  
	    setHeight(140);
	    setButtons("");
	    setFrame(true);
	    setClosable(false);
	    setPlain(true);
	    add(f);
	    addButton(ac);
	    addButton(cn);
	}
    
    public void addForm(){
    	f = new FormPanel();
    	f.setHeaderVisible(false);
      	us = new TextField<String>();
 	    us.setFieldLabel("Usuario");
 	    us.setAllowBlank(false);
 	    us.setMinLength(5);
 	    
 	    us.addKeyListener(new KeyListener(){
 		public void componentKeyDown(ComponentEvent event){
 			if(event.getKeyCode() == KeyCodes.KEY_ENTER){	
 			  if(us.isValid()){ ps.focus();}  
 			  else {us.clear(); us.focus();} 
 				
 			}
 		 }
 	    });
 	   //public static final int OnMouseOut
		//public void componentMouse
 	    
 	 	ps = new TextField<String>();
 	 	ps.setPassword(true);
 	 	ps.setFieldLabel("Password");
 	 	ps.setAllowBlank(false);
 	 	
 	 	ps.addKeyListener(new KeyListener(){
 			public void componentKeyDown(ComponentEvent event){
 				if(event.getKeyCode() == KeyCodes.KEY_ENTER){									
 					ps.focus();
 				}
 			 }
 		    });
 	 	ac  = new Button("Aceptar");

 	 	ac.addSelectionListener(new SelectionListener<ButtonEvent>() {
 		      public void componentSelected(ButtonEvent ce) {
 		    	 status();
 			     status.show();
 		    	 Usuario u = new Usuario();
 		    	 u.setUsuario(us.getValue());
 		    	 u.setPassword(ps.getValue());
 		    	 System.out.println("entro en UsuarioForm");
 		    	 Usuarios.validar(u);
		    	 Usuarios.uf = UsuarioForm.this;
 		      }
 		  });
 	 	cn = new Button("Cancelar");
 	 	cn.addSelectionListener(new SelectionListener<ButtonEvent>(){
 	 		public void componentSelected(ButtonEvent ce){
 	 		   cleanForm();
 	 		   us.focus();
 	 		}
 	 	});
   	 	f.add(us,new FormData("-20"));
 	 	f.add(ps,new FormData("-20"));
    }
    
    public void cleanForm(){
    	us.clear();
    	ps.clear();
    }

    public void status(){
      ac.setEnabled(false);
      cn.setEnabled(false);
      status = new Status();
	  status.setBusy("Guardando...");
	  status.hide();
	  status.setAutoWidth(true);
	  getButtonBar().add(status);
	  getButtonBar().add(new FillToolItem());
    }

}